package com.etoak.common.core.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * REST 接口返回结果
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultVO<T> {

    /**
     * 执行成功后的返回码
     */
    public static final int DEFAULT_SUCCESS_CODE = 200;

    /**
     * 执行成功后的返回消息
     */
    public static final String DEFAULT_SUCCESS_MESSAGE = "success";

    /**
     * 执行失败的返回码
     */
    public static final int DEFAULT_FAILED_CODE = 500;

    /**
     * 系统未知错误
     */
    public static final String INTERNAL_SYSTEM_ERROR = "系统异常";

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 添加、更新、删除接口调用
     */
    public static <T> ResultVO<T> success() {
        return success(null);
    }

    /**
     * 查询接口调用
     */
    public static <T> ResultVO<T> success(T data) {
        return new ResultVO<>(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, data);
    }

    public static <T> ResultVO<T> failed() {return failed(INTERNAL_SYSTEM_ERROR);}

    public static <T> ResultVO<T> failed(String message) {
        return failed(DEFAULT_FAILED_CODE, message);
    }

    public static <T> ResultVO<T> failed(int code, String message) {
        return new ResultVO<>(code, message, null);
    }

}